package com.k2data.k2app.util.activeMq;

import com.k2data.k2app.domain.vo.Job;
import com.k2data.k2app.service.JobService;
import lombok.extern.log4j.Log4j2;
import org.apache.activemq.Message;
import org.apache.activemq.command.ActiveMQMapMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by clb on 17-7-4.
 *
 */
@Log4j2
@Component
public class Consumer {

	@Value("${pas.proxy.queue}")
	private String queue;

	@Autowired
	JobService jobService;

	/**使用@JmsListener注解来监听指定的某个队列内的消息,是否有新增,有的话则取出队列内消息
	 *进行处理
	 *
	 **/
	@JmsListener(destination="${pas.proxy.queue}")
	public void removeMessage(final Message message){
		if (log.isDebugEnabled()) {
			log.debug("Receiving message : " + message.toString());
		}
		try {
			log.debug(message);
			if (!(message instanceof ActiveMQMapMessage)) {
				log.error("Receiving error format message " + message.toString() + " in " + queue + ".");
				return;
			}
			log.debug("消费消息！");
			Map map = ((ActiveMQMapMessage) message).getContentMap();
			log.debug("消息内容：");
			log.debug(map);
			Long jobId = Long.parseLong(map.get("instanceId").toString());
			Job job = jobService.get(jobId);
			String status = job.getResultStatus();
			jobService.updateStatus(jobId,status);
		} catch (Exception e) {
			log.error("Error happen when processing AssetMessageListener.onMessage().");
			e.printStackTrace();
		}
	}

}
